﻿namespace Restaurant
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        List<string> tableNameTextNameList = new List<string>();
        StringBuilder tableNameTextBuilder = new StringBuilder();

        public MainWindow()
        {
            System.Globalization.CultureInfo customCulture = (System.Globalization.CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
            customCulture.NumberFormat.NumberDecimalSeparator = ".";

            System.Threading.Thread.CurrentThread.CurrentCulture = customCulture;
            InitializeComponent();
            LoadArticles();
        }

        private void BarButton_Click(object sender, RoutedEventArgs e)
        {
            ChangeTableColor(sender);
        }

        private void TableButton_Click(object sender, RoutedEventArgs e)
        {
            ChangeTableColor(sender);
        }

        /// <summary>
        /// Returns the green background of the closed table
        /// </summary>
        /// <param name="tableName"></param>
        public void UpdateTableColor(string tableName)
        {
            string[] tokens = tableName.Split();
            string key = String.Format("DynamicBackground{0}", tokens[1]);
            Application.Current.Resources[key] = Brushes.Green;
        }

        /// <summary>
        /// Turns the green background of the table to red
        /// </summary>
        /// <param name="obj"></param>
        private void ChangeTableColor(object obj)
        {
            TableWindow tableWindow = new TableWindow();
            tableWindow.Title = String.Format("Table {0}", ((obj as Button).Content));
            string key = String.Format("DynamicBackground{0}", (obj as Button).Content);
            Application.Current.Resources[key] = Brushes.Red;
            tableWindow.Show();
        }

        /// <summary>
        /// Loads all articles from a file and stores them in ItemControl
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LoadArticles()
        {
            StringBuilder articlesBuilder = FileReader.ReadFile("../../articles.txt", "error.log");
            string[] articles = articlesBuilder.ToString().Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
            List<Article> allArticles = new List<Article>();

            foreach (var articleRow in articles)
            {
                string[] token = articleRow.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                token[0] = "1";
                int articleID = int.Parse(token[0]);
                decimal sellingPrice = decimal.Parse(token[3].Trim());
                decimal buyingPrice = decimal.Parse(token[4].Trim());
                double quantity = double.Parse(token[5].Trim());

                switch (token[1].Trim())
                {
                    case "Drink":
                        allArticles.Add(new Drink(articleID, token[2], sellingPrice, buyingPrice, quantity));
                        break;
                    case "Salad":
                        allArticles.Add(new Salad(articleID, token[2], sellingPrice, buyingPrice, quantity));
                        break;
                    case "Soup":
                        allArticles.Add(new Soup(articleID, token[2], sellingPrice, buyingPrice, quantity));
                        break;
                    case "Meal":
                        allArticles.Add(new Meal(articleID, token[2], sellingPrice, buyingPrice, quantity));
                        break;
                    case "Dessert":
                        allArticles.Add(new Dessert(articleID, token[2], sellingPrice, buyingPrice, quantity));
                        break;
                    default:
                        allArticles.Add(new Drink(articleID, token[2], sellingPrice, buyingPrice, quantity));
                        break;
                }
                Articles_grid.ItemsSource = allArticles;

                Articles_grid.Items.Refresh();

            }
        }
    }

}